package com.wcgksoft.controller.insect;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.insect.MedicamentType;
import com.wcgksoft.service.insect.MedicamentTypeService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
  * @Description: 药剂类型   
  * @Author: dell 
  * @Date: 2020/4/3  
  */  
@Controller
@RequestMapping("MedicamentType")
public class MedicamentTypeController {

    
    @Autowired
    private MedicamentTypeService medicamentTypeService;
    //查询药剂类型
    @ResponseBody
    @RequestMapping(value = "findMedicamentType")
    public Layui find1(@RequestParam(value = "page", required = false) Integer page,
                       @RequestParam(value = "limit", required = false) Integer limit) {

        DetachedCriteria criteria = DetachedCriteria.forClass(MedicamentType.class);

        List<MedicamentType> list = medicamentTypeService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", medicamentTypeService.getTotalCount(), list);
    }



    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {

            MedicamentType medicamentType = (MedicamentType) JSONObject.toBean(JSONObject.fromObject(data), MedicamentType.class);
            medicamentTypeService.saveOrUpdate(medicamentType);
            return Layui.data(0, "修改成功", 1, Layui.addList(medicamentType));
        }
    }


    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        MedicamentType byId = medicamentTypeService.findById(id);
        medicamentTypeService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }
}
